
#include <vector>
#include "avatar.h"
#include "commandTable.h"
#include "commandTable-default.h"
#include "display.h"
#include "handler.h"
#include "symphony.h"
#include "world.h"



/************************************************************ BASE CLASS HANDLE METHOD ************************************************************/
void Handler::handle( const std::string& text ) {
  CommandTable* commands = creature()->handler()->commandTable();
  Command* command = NULL;
  std::string input = text;

  if ( input == "!" ) {
    input = lastInput();
  } else {
    lastInput( input );
  }

  // Find the command...
  if ( ( command = commands->find( input ) ) == NULL ) {
    creature()->send( "Huh?" );
    return;
  }

  // Execute the command (if able to do so)...
  if ( !commands->execute( creature(), command, input ) ) {
    creature()->send( "Huh?" );
    return;
  }

  return;
}

void Handler::handle( void ) { // public
  handle( ((Avatar*)creature())->getInput() );
  return;
}

/************************************************************ LOGIN (BANNER) HANDLER ************************************************************/
void SendBannerHandler::activate( void ) {
  creature()->send( prompt() );
  creature()->replaceHandler( new GetNameHandler( creature() ) );
  return;
}

void SendBannerHandler::deactivate( void ) {
  return;
}

void SendBannerHandler::handle( void ) {
  return;
}

std::string SendBannerHandler::prompt( void ) {
  return "\n\n\n\n\n{RS{Gy{Ym{Bp{Mh{Co{Rn{Gy{W!!{x\n\n\n\n\n";
}



/************************************************************ LOGIN (NAME) HANDLER ************************************************************/
void GetNameHandler::activate( void ) {
  return;
}

void GetNameHandler::deactivate( void ) {
  return;
}

void GetNameHandler::handle( void ) {
  Avatar* avatar = (Avatar*)creature();
  std::string input = avatar->getInput();
  input = Regex::trim( input.substr( 0, input.find_first_of(' ') ) );

  // Check to make sure that name isn't already signed in...
  if ( World::Instance().findAvatar( input ) ) {
    avatar->send( "Sorry - they're already signed in.\n" );
    avatar->disconnected( true );
    return;
  }

  // Figure out whether or not the given name already exists...
  avatar->name( input );
  if ( avatar->load() ) {
    creature()->replaceHandler( new GetPasswordHandler( creature() ) );
  } else {
    /* This block will eventually push a new handler for creation; for now it just d/c's */
    creature()->send( "That name is not recognized.\n" );
    creature()->disconnected( true );
  }

  return;
}

std::string GetNameHandler::prompt( void ) {
  return "Name: ";
}



/************************************************************ LOGIN (PASSWORD) HANDLER ************************************************************/
void GetPasswordHandler::activate( void ) {
  creature()->send( "\n" );
  return;
}

void GetPasswordHandler::deactivate( void ) {
  return;
}

void GetPasswordHandler::handle( void ) {
  Avatar* avatar = (Avatar*)creature();
  std::string input = avatar->getInput();
  estring fd = avatar->socket()->getFd();
  input = Regex::trim( input.substr( 0, input.find_first_of(' ') ) );
  CmdLook look;

  if ( avatar->checkPassword( input ) ) {
    World::Instance().bigBrother( avatar, ADMIN_BIGBRO_LOGINS, "%s has logged in from %s.", creature()->name().c_str(), avatar->socket()->getIP().c_str() );
    World::Instance().playerLog( NOTICE, LOG_PLAYER, "%s (%lu) logged in from %s", creature()->name().c_str(), creature()->ID(), avatar->socket()->getIP().c_str() );
    World::Instance().removeAvatar( fd );
    World::Instance().insert( avatar );
    avatar->status( CONNECTED );
    creature()->replaceHandler( new InputHandler( creature() ) );
    creature()->send( "\nWecome back!\n" );
    look.execute( creature(), "" );
  } else {
    creature()->send( "That password is invalid.\n" );
    avatar->disconnected( true );
  }

  return;
}

std::string GetPasswordHandler::prompt( void ) {
  return "Password: ";
}



/************************************************************ MAIN HANDLER ************************************************************/
void InputHandler::activate( void ) {
  commandTable( &(Commands::Instance()) );
  return;
}

void InputHandler::deactivate( void ) {
  return;
}

std::string InputHandler::prompt( void ) {
  return "\n\n{W[{CSymphony{W] ";
}

/************************************************************ AEDIT HANDLER ************************************************************/
void AeditHandler::activate( void ) {
  commandTable( &(AeditCommands::Instance()) );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has entered area editing mode.", creature()->name().c_str() );
  return;
}

void AeditHandler::deactivate( void ) {
  creature()->send( "Goodbye!" );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has left area editing mode.", creature()->name().c_str() );
  return;
}

std::string AeditHandler::prompt( void ) {
  char buffer[MAX_BUFFER];
  sprintf( buffer, "\n\n{W[{cAedit {x- {G%s{W] ", creature()->aedit()->name().c_str() );
  return buffer;
}

/************************************************************ MEDIT HANDLER ************************************************************/
void MeditHandler::activate( void ) {
  commandTable( &(MeditCommands::Instance()) );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has entered mob editing mode.", creature()->name().c_str() );
  return;
}

void MeditHandler::deactivate( void ) {
  creature()->send( "Goodbye!" );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has left mob editing mode.", creature()->name().c_str() );
  return;
}

std::string MeditHandler::prompt( void ) {
  char buffer[MAX_BUFFER];
  sprintf( buffer, "\n\n{W[{cMedit {x- {G%lu{W] ", creature()->medit()->vnum() );
  return buffer;
}

/************************************************************ OEDIT HANDLER ************************************************************/
void OeditHandler::activate( void ) {
  commandTable( &(OeditCommands::Instance()) );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has entered object editing mode.", creature()->name().c_str() );
  return;
}

void OeditHandler::deactivate( void ) {
  creature()->send( "Goodbye!" );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has left object editing mode.", creature()->name().c_str() );
  return;
}

std::string OeditHandler::prompt( void ) {
  char buffer[MAX_BUFFER];
  sprintf( buffer, "\n\n{W[{cOedit {x- {G%lu{W] ", creature()->oedit()->vnum() );
  return buffer;
}

/************************************************************ PEDIT HANDLER ************************************************************/
void PeditHandler::activate( void ) {
  commandTable( &(PeditCommands::Instance()) );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has entered player editing mode.", creature()->name().c_str() );
  return;
}

void PeditHandler::deactivate( void ) {
  creature()->send( "Goodbye!" );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has left player editing mode.", creature()->name().c_str() );
  return;
}

std::string PeditHandler::prompt( void ) {
  char buffer[MAX_BUFFER];
  sprintf( buffer, "\n\n{W[{cPedit {x- {G%s{W] ", creature()->pedit()->name().c_str() );
  return buffer;
}

/************************************************************ REDIT HANDLER ************************************************************/
void ReditHandler::activate( void ) {
  commandTable( &(ReditCommands::Instance()) );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has entered room editing mode.", creature()->name().c_str() );
  return;
}

void ReditHandler::deactivate( void ) {
  creature()->send( "Goodbye!" );
  World::Instance().bigBrother( creature(), ADMIN_BIGBRO_MODES, "%s has left room editing mode.", creature()->name().c_str() );
  return;
}

std::string ReditHandler::prompt( void ) {
  char buffer[MAX_BUFFER];
  sprintf( buffer, "\n\n{W[{cRedit {x- {G%lu{W] ", creature()->room()->vnum() );
  return buffer;
}

/************************************************************ TEDIT HANDLER ************************************************************/
void TeditHandler::activate( void ) {
  commandTable( &(TeditCommands::Instance()) );
  creature()->send( "Welcome to the Symphony Text Editor." );
  return;
}

void TeditHandler::deactivate( void ) {
  std::vector<std::string>* v = (std::vector<std::string>*)getState()["vector"];
  std::string* s = (std::string*)getState()["name"];
  ((std::string*)getState()["pointer"])->assign( Display::refactor(*v) );
  delete v;
  delete s;
  getState().clear();
  creature()->send( "You've left the Symphony Text Editor." );
  return;
}

std::string TeditHandler::prompt( void ) {
  char buffer[MAX_BUFFER];
  sprintf( buffer, "\n\n{W[{cCurrently Editing: {C%s{W] ", ((std::string*)getState()["name"])->c_str() );
  return buffer;
}

